package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.CheckGroupService;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @outhor
 * @date 2020/1/13 12:34
 */

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CheckGroupService checkGroupService;

    @Override
    public void add(Setmeal setmeal) {
        //1.添加套餐基本数据(返回主键)-自增主键回填
        setmealMapper.add(setmeal);
        //        //2.添加setmeal和checkgroup关系表
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        Integer setmealId = setmeal.getId();
        //需判断如果没有勾选检查组信息,那么就不插入该表
        if (CollectionUtil.isNotEmpty(checkgroupIds)) {
            setmealMapper.SetmealAndCheckGroupAdd(setmealId, checkgroupIds);
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Setmeal> setmealList = setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), setmealList);
    }

    @Override
    public void delete(Integer id, String img) {
        //1.删除中间表数据
        setmealMapper.delSetmealAndCheckgroup(id);
        //2.删除t_setmeal表中基本数据
        setmealMapper.delete(id);
        //3.删除七牛网上的照片
        QiniuUtils.deleteFileFromQiniu(img);

    }

    @Override
    public Map findById(Integer id) {
        //1.查询基本套餐信息
        Setmeal setmeal = setmealMapper.findById(id);
        //2.查询中间表信息
        List<Integer> checkGroupIds = setmealMapper.findSetmealAndCheckgroupById(id);
        //3.查询所有检查组
        List<CheckGroup> checkGroupList = checkGroupService.findAll();
        Map map = new HashMap<>();
        map.put("setmeal", setmeal);
        map.put("checkGroupIds", checkGroupIds);
        map.put("checkGroupList", checkGroupList);
        return map;
    }

    @Override
    public void edit(Setmeal setmeal) {
        //1.编辑基本信息
        setmealMapper.edit(setmeal);
        //2.删除中间表信息
        setmealMapper.delSetmealAndCheckgroup(setmeal.getId());
        //3.添加中间表信息
        List<Integer> checkgroupIds = setmeal.getCheckgroupIds();
        if (CollectionUtil.isNotEmpty(checkgroupIds)) {
            setmealMapper.SetmealAndCheckGroupAdd(setmeal.getId(), checkgroupIds);
        }
        if (setmeal.getOldImgName() != null  && (!setmeal.getOldImgName().equals(setmeal.getImg()))) {
            QiniuUtils.deleteFileFromQiniu(setmeal.getOldImgName());
        }
    }

    @Override
    public List<Setmeal> findSetmealALL() {
        return setmealMapper.findSetmealALL();
    }

    @Override
    public Setmeal findSetmealWechatById(Integer id) {
        Setmeal setmeal = setmealMapper.findSetmealWechatById(id);
        if (setmeal != null){
            List<CheckGroup> checkGroupList = setmealMapper.findSetmealCheckgroupWechatById(id);
            if (CollectionUtil.isNotEmpty(checkGroupList)){
                //方法一,需要循环查询
//                for (CheckGroup checkGroup : checkGroupList) {
//                    List<CheckItem> checkItemList = setmealMapper.findSetmealCheckItemWechatById(checkGroup.getId());
//                    checkGroup.setCheckItems(checkItemList);
//                }


                //方法二,对循环查询的优化
                //新建一个集合收集所有要查询的checkGroupId,并将ID放入集合中
                List<Integer> checkGroupIds = new ArrayList<>();
                //遍历查询出的checkGroup中的id,将所有id存放到checkGroupIds的集合中
                for (CheckGroup checkGroup : checkGroupList) {
                    checkGroupIds.add(checkGroup.getId());
                }
                //使用checkGroup的ID集合查询checkItemList列表(查询出列表中包含checkGroupId和checkItem信息)
                List<CheckItem> checkItemList = setmealMapper.findCheckItemByCheckGroupIdBatch(checkGroupIds);
                //新建个map存放格式为上面查询列表格式
                Map<Integer,List<CheckItem>> map = new HashMap<>();
                for (CheckItem checkItem : checkItemList) {
                    //从checkItem提出checkGroupId
                    Integer checkGroupId = checkItem.getCheckGroupId();
                    //根据checkGroupId在map中提取map中的checkItem,第一次是空map必定是空的checkItem
                    List<CheckItem> items = map.get(checkGroupId);
                    if (items == null){
                        items = new ArrayList<>();
                        items.add(checkItem);
                        map.put(checkGroupId,items);
                    }else {
                        items.add(checkItem);
                    }
                }
//                方式三
//                map =  checkItemList.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));
                for (CheckGroup checkGroup : checkGroupList) {
                    List<CheckItem> checkItems = map.get(checkGroup.getId());
                    checkGroup.setCheckItems(checkItems); 
                }
            }
            setmeal.setCheckGroups(checkGroupList);
        }
        return setmeal;
    }
}

